﻿using JY.Owin.Security.mixi;
using System;

namespace Owin
{
    public static class MixiAuthenticationExtensions
    {
        /// <summary>
        /// Authenticate users using mixi
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <param name="options">Middleware configuration options</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseMixiAuthentication(this IAppBuilder app, MixiAuthenticationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            app.Use(typeof(MixiAuthenticationMiddleware), app, options);
            return app;
        }

        /// <summary>
        /// Authenticate users using mixi
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseMixiAuthentication(
            this IAppBuilder app)
        {
            return UseMixiAuthentication(
                app,
                new MixiAuthenticationOptions());
        }

        /// <summary>
        /// Authenticate users using mixi OAuth 2.0
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <param name="consumerKey">The mixi assigned consumer key</param>
        /// <param name="consumerSecret">The mixi assigned consumer secret</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseMixiAuthentication(
            this IAppBuilder app,
            string consumerKey,
            string consumerSecret)
        {
            return UseMixiAuthentication(
                app,
                new MixiAuthenticationOptions
                {
                    ClientId = consumerKey,
                    ClientSecret = consumerSecret
                });
        }
    }
}
